package com.example.jn_dxs.Base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.jn_dxs.Utlis.LoadingUtil;
import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.ParameterizedType;

public abstract class BaseFragment<M extends BaseViewModel, B extends ViewDataBinding> extends Fragment implements TabLayout.OnTabSelectedListener {

    private boolean isViewCreated;
    private boolean isViewVisible;
    protected boolean isDataLoaded;
    protected int requestTag = 1;

    protected String TAG;
    protected Activity activity;
    protected B binding;
    protected M model;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getClass().getSimpleName();
        if (activity == null && getActivity() != null) {
            activity = getActivity();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        model = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(activity.getApplication())).get((Class<M>) (((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
        binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        model.msg.observe(this, this::onMsg);
        model.loadingStatus.observe(this, integer -> LoadingUtil.setStatus(activity, integer));
        model.dataArray.observe(this, this::onArrayDataChange);
        model.dataObject.observe(this, this::onObjectDataChange);
        return binding.getRoot();
    }

    protected void onMsg(String msg) {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
    }


    protected void onArrayDataChange(JSONArray array) {
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadOnViewCreated();
    }

    protected abstract int getLayoutId();

    protected abstract void loadData();

    protected void onObjectDataChange(JSONObject object) {
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isViewVisible = isVisibleToUser;
        checkToLoadData();
    }

    private void checkToLoadData() {
        if (isViewCreated && isViewVisible && !isDataLoaded) {
            loadData();
        }
    }

    private void loadOnViewCreated() {
        isViewCreated = true;
        checkToLoadData();
    }

    protected void complete() {
        isViewCreated = isViewVisible = isDataLoaded = true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }


}
